global basedir "~/Dropbox/heyTim/UKKeynesians/UKKeynesiansReplication"
global texdir "$basedir/generated_tex"
global imgdir "$basedir/generated_images"
global datadir "$basedir/generated_data"

clear
insheet using "$basedir/source_data/exp2_UCLResults_160704_Economy.csv"

gen     incomeDropPrimed = 0
replace incomeDropPrimed = 1 if q1 < 9
replace incomeDropPrimed = 1 if q4 < 9

gen     reduceDeficit = q2 if q2 < 9
replace reduceDeficit = q3 if q3 < 9 & reduceDeficit == .
replace reduceDeficit = q5 if q5 < 9 & reduceDeficit == .
replace reduceDeficit = q6 if q6 < 9 & reduceDeficit == .

gen     goodMacro = .
replace goodMacro = 1 if q2 < 9
replace goodMacro = 1 if q3 < 9
replace goodMacro = 0 if q5 < 9
replace goodMacro = 0 if q6 < 9

label define Lab_GoodMacro 0 "$ Econ_i $: Bad" 1 "$ Econ_i $: Good"
label values goodMacro Lab_GoodMacro


gen     grossHHInc = 2.5  if profile_gross_household == 1
replace grossHHInc = 7.5  if profile_gross_household == 2
replace grossHHInc = 12.5 if profile_gross_household == 3
replace grossHHInc = 17.5 if profile_gross_household == 4
replace grossHHInc = 22.5 if profile_gross_household == 5
replace grossHHInc = 27.5 if profile_gross_household == 6
replace grossHHInc = 32.5 if profile_gross_household == 7
replace grossHHInc = 37.5 if profile_gross_household == 8
replace grossHHInc = 42.5 if profile_gross_household == 9
replace grossHHInc = 47.5 if profile_gross_household == 10
replace grossHHInc = 55   if profile_gross_household == 11
replace grossHHInc = 65   if profile_gross_household == 12
replace grossHHInc = 85   if profile_gross_household == 13
replace grossHHInc = 125  if profile_gross_household == 14
replace grossHHInc = 150  if profile_gross_household == 15
replace grossHHInc = 0.1  if profile_gross_household > 15

gen     logGrossHHInc = log(grossHHInc)

gen     grossHHIncdk = 1 if profile_gross_household > 15
replace grossHHIncdk = 0 if profile_gross_household <= 15

gen     nopid = 1 if partyid_2015 == 10
replace nopid = 0 if partyid_2015 < 10


recode partyid_2015 (1 3=1) (2 4 5 7 = 2) (6 8 9 = 3) (10 99 = 4), gen(pidgroup)
label define Lab_PIdGroup 1 "$ PId_i $: C/LD" 2 "$ PId_i $: Left" 3 "$ PId_i $: Other" 4 "$ PId_i $: NA"
label values pidgroup Lab_PIdGroup


rename w8 W8


label define Lab_Vote 1 "$ Voted_i $: Con" 2 "$ Voted_i $: Lab" 3 "$ Voted_i $: LD" 4 "$ Voted_i $: SNP" 5 "$ Voted_i $: PC" 6 "$ Voted_i $: UKIP" 7 "$ Voted_i $: Green" 8 "$ Voted_i $: BNP" 9 "$ Voted_i $: Other" 99 "$ Voted_i $: DK" 999 "$ Voted_i $: N/A"
label values pastvote_2015 Lab_Vote

label define Lab_Newspaper 1 "$ Newspaper:$ Express" 2 "$ Newspaper:$ Mail" 3 "$ Newspaper:$ Mirror" 4 "$ Newspaper:$ Star" 5 "$ Newspaper:$ Sun" 6 "$ Newspaper:$ Telegraph" 7 "$ Newspaper:$ FT" 8 "$ Newspaper:$ Guardian" 9 "$ Newspaper:$ Independent" 10 "$ Newspaper:$ Times" 11 "$ Newspaper:$ Scotsman" 12 "$ Newspaper:$ Herald" 13 "$ Newspaper:$ Western Mail" 14 "$ Newspaper:$ Other local" 15 "$ Newspaper:$ Other" 16 "$ Newspaper:$ None"
label values profile_newspaper_readership Lab_Newspaper

label define Lab_GrossHH 1 "$ HHInc_i $: <5k" 2 "$ HHInc_i $: 5-10k" 3 "$ HHInc_i $: 10-15k" 4 "$ HHInc_i $: 15-20k" 5 "$ HHInc_i $: 20-25k" 6 "$ HHInc_i $: 25-30k" 7 "$ HHInc_i $: 30-35k" 8 "$ HHInc_i $: 35-40k" 9 "$ HHInc_i $: 40-45k" 10 "$ HHInc_i $: 45-50k" 11 "$ HHInc_i $: 50-60k" 12 "$ HHInc_i $: 60-70k" 13 "$ HHInc_i $: 70-100k" 14 "$ HHInc_i $: 100-150k" 15 "$ HHInc_i $: >150k" 16 "$ HHInc_i $: DK" 17 "$ HHInc_i $: N/R" 98 "$ HHInc_i $: N/A"
label values profile_gross_household Lab_GrossHH



reg reduceDeficit i.goodMacro [pweight=W8]
local exp2_N = e(N)
file open  afile using "$texdir/exp2_N.tex", write replace
file write afile "`exp2_N'"
file close afile
estimates store Exp2BaseOLS
reg reduceDeficit i.goodMacro i.pastvote_2015 b16.profile_newspaper_readership i.profile_gross_household [pweight=W8]
estimates store Exp2ContOLS

ologit reduceDeficit i.goodMacro [pweight=W8]
estimates store Exp2BaseOLo
ologit reduceDeficit i.goodMacro i.pastvote_2015 b16.profile_newspaper_readership i.profile_gross_household [pweight=W8]
estimates store Exp2ContOLo

reg reduceDeficit i.goodMacro##i.pidgroup [pweight=W8]
estimates store Exp2PIdBaseOLS
reg reduceDeficit i.goodMacro##i.pidgroup  i.pastvote_2015 b16.profile_newspaper_readership i.profile_gross_household [pweight=W8]
estimates store Exp2PIdContOLS

ologit reduceDeficit i.goodMacro##i.pidgroup [pweight=W8]
estimates store Exp2PIdBaseOLo
ologit reduceDeficit i.goodMacro##i.pidgroup  i.pastvote_2015 b16.profile_newspaper_readership i.profile_gross_household [pweight=W8]
estimates store Exp2PIdContOLo


global estout_drop_pidgroups drop(0.goodMacro#1.pidgroup 0.goodMacro#2.pidgroup 0.goodMacro#3.pidgroup 0.goodMacro#4.pidgroup)
global estout_drop_pid       drop(0.goodMacro#1.partyid_2015 0.goodMacro#2.partyid_2015 0.goodMacro#3.partyid_2015 0.goodMacro#4.partyid_2015 0.goodMacro#5.partyid_2015 0.goodMacro#6.partyid_2015 0.goodMacro#7.partyid_2015 0.goodMacro#8.partyid_2015 0.goodMacro#9.partyid_2015 0.goodMacro#10.partyid_2015 0.goodMacro#99.partyid_2015)
global estout_options label replace stats(N r2 r2_p ll, labels("$ N $" "$ R^2 $" "$ Pseudo R^2 $" "Log Lik.")) cells( " b(fmt(a2)) se(fmt(2))" ) starlevels(* 0.10 ** 0.05 *** 0.01) style(tex) posthead("\hline") prefoot(\hline) postfoot("\hline") mlabels("(1)" "(2)" "(3)" "(4)" "(5)" "(6)")
global estout_ologit_options varlabels(cut1:_cons "Cut 1" cut2:_cons "Cut 2" cut3:_cons "Cut 3" cut4:_cons "Cut 4" _cons "Constant") eqlabels("", none)

estout  Exp2BaseOLS		Exp2BaseOLo		Exp2PIdBaseOLS		Exp2PIdBaseOLo		using "$texdir/exp2.tex",			$estout_options $estout_ologit_options $estout_drop_pidgroups
estout  Exp2ContOLS		Exp2ContOLo		Exp2PIdContOLS		Exp2PIdContOLo		using "$texdir/exp2_controls.tex",	$estout_options $estout_ologit_options $estout_drop_pidgroups


label define Lab_VotePlain 1 "Voted: Con" 2 "Voted: Lab" 3 "Voted: LD" 4 "Voted: SNP" 5 "Voted: PC" 5 "Voted: UKIP" 6 "Voted: Green" 7 "Voted: BNP" 8 "Voted: Other" 9 "Voted: DK" 10 "Voted: N/A"
label values pastvote_2015 Lab_VotePlain


label define Lab_NewspaperPlain 1 "Newspaper: Express" 2 "Newspaper: Mail" 3 "Newspaper: Mirror" 4 "Newspaper: Star" 5 "Newspaper: Sun" 6 "Newspaper: Telegraph" 7 "Newspaper: FT" 8 "Newspaper: Guardian" 9 "Newspaper: Independent" 10 "Newspaper: Times" 11 "Newspaper: Scotsman" 12 "Newspaper: Herald" 13 "Newspaper: Western Mail" 14 "Newspaper: Other local" 15 "Newspaper: Other" 16 "Newspaper: None"
label values profile_newspaper_readership Lab_NewspaperPlain


local keepers  1.goodMacro
local controlkeepers 1.pastvote_2015 2.pastvote_2015 3.pastvote_2015 16.profile_newspaper_readership 6.profile_newspaper_readership 8.profile_newspaper_readership
local renamers 1.goodMacro = "Treat: Good Econ"
coefplot (Exp2BaseOLS, label(Base) keep(`keepers') rename(`renamers'))  (Exp2ContOLS, label(+Controls) keep(`keepers' `controlkeepers') rename(`renamers')), xline(0) scheme(plotplain) cismooth levels(95) baselevels
graph export "$imgdir/exp2_coefplot.pdf", replace

local keepers  1.goodMacro 1.goodMacro#2.pidgroup 1.goodMacro#3.pidgroup 1.goodMacro#4.pidgroup
local renamers 1.goodMacro = "Party Id: Con/LD" 1.goodMacro#2.pidgroup = "Party Id: Lab/SNP/PC/Green" 1.goodMacro#3.pidgroup = "Party Id: UKIP/BNP/Other" 1.goodMacro#4.pidgroup = "Party Id: NA"
coefplot (Exp2PIdBaseOLS, label(Base) keep(`keepers') rename(`renamers')) (Exp2PIdContOLS, label(With controls) keep(`keepers') rename(`renamers')), xline(0) scheme(plotplain) cismooth levels(95)
graph export "$imgdir/exp2_pid_coefplot.pdf", replace


gen exp2 = 1

keep reduceDeficit goodMacro pastvote_2015 partyid_2015 profile_newspaper_readership profile_gross_household pidgroup W8 exp2
append using "$datadir/exp1.dta"

replace exp2 = 0 if exp2 == .
label define Lab_Exp2 0 "Experiment: 1" 1 "Experiment: 2"
label values exp2 Lab_Exp2

label values pastvote_2015 Lab_Vote
label values profile_newspaper_readership Lab_Newspaper

label define Lab_PId      1 "$ PId_i$: Con" 2 "$ PId_i$: Lab" 3 "$ PId_i$: LD" 4 "$ PId_i$: SNP" 5 "$ PId_i$: PC" 6 "$ PId_i$: UKIP" 7 "$ PId_i$: Green" 8 "$ PId_i$: BNP" 9 "$ PId_i$: Other" 10 "$ PId_i$: None" 99 "$ PId_i$: DK"
label values partyid_2015 Lab_PId
label define Lab_PIdPlain 1 "PId: Con" 2 "PId: Lab" 3 "PId: LD" 4 "PId: SNP" 5 "PId: PC" 6 "PId: UKIP" 7 "PId: Green" 8 "PId: BNP" 9 "PId: Other" 10 "PId: DK" 99 "PId: None"

reg reduceDeficit i.goodMacro i.exp2 [pweight=W8]
estimates store ExpXBaseOLS
reg reduceDeficit i.goodMacro i.exp2 i.pastvote_2015 b16.profile_newspaper_readership i.profile_gross_household i.exp2 [pweight=W8]
estimates store ExpXContOLS

ologit reduceDeficit i.goodMacro i.exp2 [pweight=W8]
estimates store ExpXBaseOLo
ologit reduceDeficit i.goodMacro i.pastvote_2015 b16.profile_newspaper_readership i.profile_gross_household i.exp2 [pweight=W8]
estimates store ExpXContOLo

reg reduceDeficit i.goodMacro##i.pidgroup i.exp2 [pweight=W8]
estimates store ExpXPIdBaseOLS
reg reduceDeficit i.goodMacro##i.pidgroup i.exp2 i.pastvote_2015 b16.profile_newspaper_readership i.profile_gross_household i.exp2 [pweight=W8]
estimates store ExpXPIdContOLS

ologit reduceDeficit i.goodMacro##i.pidgroup i.exp2 [pweight=W8]
estimates store ExpXPIdBaseOLo
ologit reduceDeficit i.goodMacro##i.pidgroup i.pastvote_2015 b16.profile_newspaper_readership i.profile_gross_household i.exp2 [pweight=W8]
estimates store ExpXPIdContOLo

reg reduceDeficit i.goodMacro##i.partyid_2015 i.exp2 [pweight=W8]
estimates store ExpXPIdFBaseOLS
reg reduceDeficit i.goodMacro##i.partyid_2015 i.exp2 i.pastvote_2015 b16.profile_newspaper_readership i.profile_gross_household i.exp2 [pweight=W8]
estimates store ExpXPIdFContOLS

ologit reduceDeficit i.goodMacro##i.partyid_2015 i.exp2 [pweight=W8]
estimates store ExpXPIdFBaseOLo
ologit reduceDeficit i.goodMacro##i.partyid_2015 i.pastvote_2015 b16.profile_newspaper_readership i.profile_gross_household i.exp2 [pweight=W8]
estimates store ExpXPIdFContOLo


estout  ExpXBaseOLS		ExpXBaseOLo		ExpXPIdBaseOLS		ExpXPIdBaseOLo		using "$texdir/expX.tex",			$estout_options $estout_ologit_options $estout_drop_pidgroups
estout  ExpXContOLS		ExpXContOLo		ExpXPIdContOLS		ExpXPIdContOLo		using "$texdir/expX_controls.tex",	$estout_options $estout_ologit_options $estout_drop_pidgroups
estout  ExpXPIdFBaseOLS	ExpXPIdFBaseOLo	ExpXPIdFContOLS		ExpXPIdFContOLo		using "$texdir/expX_pidf.tex",		$estout_options $estout_ologit_options $estout_drop_pid


label values pastvote_2015 Lab_VotePlain
label values profile_newspaper_readership Lab_NewspaperPlain
label values partyid_2015 Lab_PIdPlain

local keepers  1.goodMacro
local controlkeepers 1.pastvote_2015 2.pastvote_2015 3.pastvote_2015 16.profile_newspaper_readership 6.profile_newspaper_readership 8.profile_newspaper_readership
local renamers 1.goodMacro = "Treat: Good Econ"
coefplot (ExpXBaseOLS, label(Base) keep(`keepers') rename(`renamers'))  (ExpXContOLS, label(+Controls) keep(`keepers' `controlkeepers') rename(`renamers')), xline(0) scheme(plotplain) cismooth levels(95) baselevels
graph export "$imgdir/expX_coefplot.pdf", replace

local keepers  1.goodMacro 1.goodMacro#2.pidgroup 1.goodMacro#3.pidgroup 1.goodMacro#4.pidgroup
local renamers 1.goodMacro = "Party Id: Con/LD" 1.goodMacro#2.pidgroup = "Party Id: Lab/SNP/PC/Green" 1.goodMacro#3.pidgroup = "Party Id: UKIP/BNP/Other" 1.goodMacro#4.pidgroup = "Party Id: None"
coefplot (ExpXPIdBaseOLS, label(Base) keep(`keepers') rename(`renamers')) (ExpXPIdContOLS, label(With controls) keep(`keepers') rename(`renamers')), xline(0) scheme(plotplain) cismooth levels(95)
graph export "$imgdir/expX_pid_coefplot.pdf", replace

local keepers  1.goodMacro 1.goodMacro#2.partyid_2015 1.goodMacro#3.partyid_2015
local renamers 1.goodMacro = "Party Id: Con" 1.goodMacro#2.partyid_2015 = "Party Id: Lab" 1.goodMacro#3.partyid_2015 = "Party Id: LD"
coefplot (ExpXPIdFBaseOLS, label(Base) keep(`keepers') rename(`renamers')) (ExpXPIdFContOLS, label(With controls) keep(`keepers') rename(`renamers')), xline(0) scheme(plotplain) cismooth levels(95)
graph export "$imgdir/expX_pidf_coefplot.pdf", replace


do "$basedir/scripts/balanceplot.do"
balance_plot, ibvars(pidgroup pastvote_2015 profile_gross_household profile_newspaper_readership) tvar(goodMacro)
mat b = r(b)
mat ci = r(ci)
coefplot matrix(b), ci(ci) keep(*:) xline(0) scheme(plotplain)
graph export "$imgdir/expX_balance_plot.pdf", replace
